<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class IdentitasSekolah extends MY_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		if($this->session->userdata('login') === TRUE && in_array(128,$this->access)){
			$log['activity']='VIEW';
			$log['information']='Masuk ke menu : '.$this->uri->uri_string();
			$this->model_log->add($log);
		}else{
			$log['activity']='VIEW';
			$log['information']='Tidak diijinkan : '.$this->uri->uri_string();
			$this->model_log->add($log);
			redirect('sessions/login');
		}
		
		$this->config->set_item("menu_active","identitas-sekolah");
		$this->config->set_item("menu_root","pengaturan-awal");
    }
	
	public function index()
	{
		$this->load->model('model_identitas_sekolah');
		$mdl_departemen = $this->load->model('departemen/model_departemen');
		
		
		$q = '';
		if(isset($_POST['departemen_id'])){
			$q = $_POST['departemen_id'];
		}
		
		$data['departemen'] = $mdl_departemen->get_departemen();
		
		$data['identitas']= $this->model_identitas_sekolah->get_identitas_by_departemen($q);
		
		
		$data['departemen_id'] = $q;
		
		$params['content'] = $this->load->view('list',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	/*function topdf () {
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		prep_pdf();
		
		$l = '';
		if(isset($_POST['laporan'])){
			$l = $_POST['laporan'];
			$data['identitas'] = $this->model_identitas_sekolah->get_identitas_by_departemen($l);
			$this->cezpdf->ezTable($data['identitas']);
			$this->cezpdf->ezStream();
			
		}
	}*/
	public function topdf($id){
		// $this->load->helper(array('dompdf', 'file'));
		 // // page info here, db calls, etc.     
		 // // $html = $this->load->view('cetak', null, false);
		 // // pdf_create($html, 'filename');
		 // $this->load->model('model_pegawai');
		// $data['row'] =$this->model_pegawai->get_pegawai_by(3);
		 // $this->load->view('cetak', $data);
		 
		 // $this->load->library('fpdf');
		 // $this->fpdf->addPage();
		 // $this->fpdf->SetFont('Arial','B','16');
		 
		 // $this->fpdf->Cell(40,10,'Hello World');
		 // $this->fpdf->Output();
		 $this->load->model('model_identitas_sekolah');
		 $data['identitas2']= $this->model_identitas_sekolah->get_identitas_by_id($id);
		 // var_dump($data);
		 $this->load->view('cetak_identitas',$data);
		 // var_dump($data);
		 
	}
	
	
	public function form($id=0,$kode=0)
	{
		$this->load->model('model_identitas_sekolah');
		$mdl_departemen=$this->load->model('departemen/model_departemen');
		
		if($id!=0){
			$id = decode($id);
			$kode = decode($kode);
		}	
		// var_dump($kode);
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			
			if(!empty($_FILES) && $_FILES['userfile']['size'] != 0){
				$config['upload_path'] = './temp/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '100';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';

				$this->load->library('upload', $config);

				if ( ! $this->upload->do_upload())
				{
					$this->session->set_flashdata('error',$this->upload->display_errors());
					redirect('identitasSekolah/form/'.encode($id));
				}
				else
				{
					$upload = $this->upload->data();
					$data['foto'] = file_get_contents($upload['full_path']);
					
					if(is_file($upload['full_path']))
						unlink($upload['full_path']);
				}
			}
			
			if($data['departemen_id'] == 0)
				$data['departemen_id'] = NULL;
			
			if($id == 0){	
				$this->model_identitas_sekolah->add($data);
				
				$log['activity']='INSERT';
				$log['information']='Insert Identitas Sekolah dengan data :'.json_encode($_POST['data']);
			}else{
				$this->model_identitas_sekolah->edit($id,$data);			
				$log['activity']='UPDATE';
				$log['information']="UPDATE Identitas Sekolah id $id dengan data :".json_encode($_POST['data']);
			}
			
			$this->model_log->add($log);
			redirect('identitasSekolah');
			
			return;
		}
		$data['data']= $this->model_identitas_sekolah->get_identitas_by_id((string)$id);
		$data['departemen_id'] = $kode;
		// var_dump($data);
		$data['departemen'] = $mdl_departemen->get_departemen();
		
		
		
		
		$params['content'] = $this->load->view('form',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function delete($id=0){
		
		$id = decode($id);
		
		if($id != 1){		
			$this->load->model('model_identitas_sekolah');			
			$this->model_identitas_sekolah->delete($id);
		}
		
		redirect('identitasSekolah');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */